package Sisth;
import java.util.Scanner;

public class User extends Person implements operate {   //user类继承了人的类，  实现operate接口

    public User(String name, String sex, int age) {
        //调用人基类的构造函数（派生类不继承基类的构造函数）
        super(name, sex, age);
    }

    Scanner sc = new Scanner(System.in);


    public void operate() {  //普通用户的操作

        System.out.print("请输入确认操作:");
        int i = sc.nextInt();
        switch (i) {
            case 1:
                //查
                select(books);
                break;
            case 2:
                //借
                delete(books);
                break;
            case 3:
                //还
                add(books);
                break;
            case 4:
                //书列表
                list(books);
                break;
            default:
                System.out.println("输入有误！");
                break;
        }
    }


    public void select(Book[] books) {
        System.out.println("请输入书名进行查询：");
        String name = sc.next();
        if (name != null) {
            boolean flag = false;
            int bookNum = 0;
            for (int i = 0; i < books.length; i++) {
                if (books[i].getName() != null && name.equals(books[i].getName())) {
                    flag = true;
                    bookNum = i;
                    break;
                }
            }
            if (!flag) {
                System.out.println("未查询到此书籍！");
            } else {
                System.out.println("书籍信息为：" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");
                System.out.println(
                        "【" + books[bookNum].getName() + "】—— 【" + books[bookNum].getAuthor() + "】——【" + books[bookNum]
                                .getPrice() + "】——【" + books[bookNum].getCategory() + "】——" + books[bookNum]
                                .getState());
            }
        }
    }


    public void delete(Book[] books) {
        System.out.println("请输入要借阅的书名:");
        String name = sc.next();
        if (name != null) {
            for (int i = 0; i < books.length - 1; i++) {
                if (books[i].getName().equals(name)) {
                    //借走书时设置状态为false
                    if (!books[i].getState()) {
                        System.out.println("【" + name + "】" + "已经被借出！");
                    } else {
                        books[i].setState(false);
                        System.out.println("借阅成功！");
                    }
                    break;
                }
            }
        }
    }


    public void add(Book[] books) {

        System.out.println("请输入要还的书名：");
        String name = sc.next();

        if (name != null) {
            boolean flag = true;
            int i = 0;
            while (flag) {
                if (books[i].getName().equals(name)) {
                    if (!books[i].getState()) {
                        books[i].setState(true);
                        System.out.println("还书成功！");
                        flag = false;
                    } else {
                        System.out.println("该书籍已经被还回！");
                        break;
                    }
                }
                i++;
            }
        }
    }


    public void list(Book[] books) {
        System.out.println("图书列表如下:" + "\n" + "【书名】—— 【作者】——【价格】——【分类】——状态");  //打印Book数组
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].price != 0) {
                    System.out.println(
                            "【" + books[i].getName() + "】—— 【" + books[i].getAuthor() + "】——【" + books[i].getPrice()
                                    + "】——【" + books[i].getCategory() + "】——" + books[i].getState());

                }
            }
        }
    }
}
